<!-- Home页面 -->
<template>
  <el-container class="homeContainer">
    <!-- 头部区域 -->
    <el-header>
      <div class="headTitle">
        <img
          src="@/assets/heima.png"
          alt="logo"
        />
        <span>电商后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside
        :class="{ smallMenu: isCollapse }"
        width="200px"
      >
        <div
          class="toggleButton"
          @click="toggleCollapse"
        >|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eef"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.path"
            v-for="item in menusList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="childItem.path"
              v-for="childItem in item.children"
              :id="childItem.id"
              @click="saveNavState(childItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ childItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主区域 -->
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menusList: [],
      iconsObj: {
        125: "iconfont icon-yonghu",
        103: "iconfont icon-quanxian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-dingdan",
        145: "iconfont icon-tongji",
      },
      isCollapse: false,
      // 当前活跃路径
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    // 将活跃的路径保存到sessionStore中，使其通过绑定default-router属性来保持对应菜单栏的高亮
    this.activePath = sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menusList = res.data;
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存当前活跃的path
    saveNavState(activePath) {
      console.log(activePath);
      sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style scoped>
.homeContainer {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  color: #eeeeee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  font-size: 20px;
}

.el-header div {
  display: flex;
  align-items: center;
}

.el-header div span {
  margin-left: 15px;
}

.el-aside {
  background-color: #333744;
  color: #eeeeee;
}

.el-main {
  background-color: #eaedf1;
  color: #333;
  text-align: center;
}

.iconfont {
  margin-right: 10px;
}

.el-menu {
  border-right: 0;
}

.toggleButton {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 20px;
  text-align: center;
  color: #ffffff;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.smallMenu {
  width: 64px !important;
}

.main {
  height: 100%;
  overflow-y: auto;
}
</style>
